	var userID = "";
	var disc_etag = "";
	var instant_etag = "";
	var tabs = null;
	var home_tab = null;
	var browse_tab = null;
	var queues_tab = null;
	var recommendations_tab = null;
	
	const top_100_rss = "http://rss.netflix.com/Top100RSS";
	const new_releases_rss = "http://rss.netflix.com/NewReleasesRSS";
	
	var current_selection = null;
		
    // Display UI depending on OAuth access state of the gadget (see <divs> above).
    // If user hasn't approved access to data, provide a "Personalize this gadget" link
    // that contains the oauthApprovalUrl returned from makeRequest.
    //
    // If the user has opened the popup window but hasn't yet approved access, display
    // text prompting the user to confirm that s/he approved access to data.  The user
    // may not ever need to click this link, if the gadget is able to automatically
    // detect when the user has approved access, but showing the link gives users
    // an option to fetch their data even if the automatic detection fails.
    //
    // When the user confirms access, the fetchData() function is invoked again to
    // obtain and display the user's data.
    function showOneSection(toshow) {
		var sections = ["main", "approval", "waiting"];
		for (var i=0; i < sections.length; ++i) {
			var s = sections[i];
			var el = document.getElementById(s);
			if (s == toshow) {
				el.style.display = "block";
			} else {
				el.style.display = "none";
			}
		}
    }
	
	/* -------------------------------------------------------------- */
	/* -------------------- Utility Methods ------------------------- */
	/* -------------------------------------------------------------- */
	function percentEncode(s) {
		if (s == null) {
			return "";
		}
		if (s instanceof Array) {
			var e = "";
			for (var i = 0; i < s.length; ++s) {
				if (e != "") e += '&';
				e += percentEncode(s[i]);
			}
			return e;
		}
		s = encodeURIComponent(s);
		// Now replace the values which encodeURIComponent doesn't do
		// encodeURIComponent ignores: - _ . ! ~ * ' ( )
		// OAuth dictates the only ones you can ignore are: - _ . ~
		// Source: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent
		s = s.replace("!", "%21", "g");
		s = s.replace("*", "%2A", "g");
		s = s.replace("'", "%27", "g");
		s = s.replace("(", "%28", "g");
		s = s.replace(")", "%29", "g");
		return s;
	}
	
	function add_to_both_queues(movie_id, loading_id) {
		add_to_queue(movie_id, "disc", loading_id);
		add_to_queue(movie_id, "instant", loading_id);
	}
	
	function add_to_queue(movieID, type, loading_id) {
		document.getElementById(loading_id).className = "loading_div_front";
		updateQueue(movieID, null, type, loading_id);
	}
	
    // Process returned JSON feed to display data.
	function showResults(result, type, loading_id, queue) {
		if (type == "recommendations") {
			showRecommendations(result, loading_id);
		} else if (type == "queue") {
			showQueue(result, queue, loading_id);
		} else if (type == "catalog") {
			showCatalog(result, loading_id);
		}
	}
	
    // Invoke makeRequest() to fetch data from the service provider endpoint.
    // Depending on the results of makeRequest, decide which version of the UI
    // to ask showOneSection() to display. If user has approved access to his
    // or her data, display data.
    // If the user hasn't approved access yet, response.oauthApprovalUrl contains a
    // URL that includes a Google-supplied request token. This is presented in the 
    // gadget as a link that the user clicks to begin the approval process.     
    function fetchData(url, type, loading_id, queue) {
		var params = {};
		params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
		params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
		params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "netflix";
		params[gadgets.io.RequestParameters.OAUTH_USE_TOKEN] = "always";
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
		gadgets.io.makeRequest(url, function (response) {
			if (response.oauthApprovalUrl) {
				var popup = shindig.oauth.popup({
					destination: response.oauthApprovalUrl + "&application_name=iGoogle+Netflix",
					windowOptions: null,
					onOpen: function() { showOneSection('waiting'); },
					onClose: function() { fetchData(url, type, loading_id, queue); }
				});
				var personalize = document.getElementById('personalize');
				personalize.onclick = popup.createOpenerOnClick();
				var approvaldone = document.getElementById('approvaldone');
				approvaldone.onclick = popup.createApprovedOnClick();
				showOneSection('approval');
			} else if (response.data) {
				showOneSection('approval');
				showResults(response.data, type, loading_id, queue);
			} else {
				alert("OAuth error: " + response.oauthError + ": " + response.oauthErrorText);
			}
		}, params);
    }
		
	function updateEtag(response, queue, loading_id) {
		var itemList = response.getElementsByTagName("status").item(0).childNodes;
		for (var i = 0; i < itemList.length ; i++) {
			if (itemList.item(i).nodeName != "#text") {
				if (itemList.item(i).nodeName == "etag") {
					if (queue == "disc") {
						disc_etag = itemList.item(i).firstChild.nodeValue;
					} else if (queue == "instant") {
						instant_etag = itemList.item(i).firstChild.nodeValue;
					}
				}
			}
		}
		if (loading_id != null) {
			document.getElementById(loading_id).className = "loading_div_back";
		}
	}
	
	function postData(url, postdata, queue, loading_id) {
		var params = {};
		params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
		params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
		params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "netflix";
		params[gadgets.io.RequestParameters.OAUTH_USE_TOKEN] = "always";
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
		params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);
		gadgets.io.makeRequest(url, function (response) {
			if (response.oauthApprovalUrl) {
				var popup = shindig.oauth.popup({
					destination: response.oauthApprovalUrl + "&application_name=iGoogle+Netflix",
					windowOptions: null,
					onOpen: function() { showOneSection('waiting'); },
					onClose: function() { fetchData(url, type, loading_id, queue); }
				});
				var personalize = document.getElementById('personalize');
				personalize.onclick = popup.createOpenerOnClick();
				var approvaldone = document.getElementById('approvaldone');
				approvaldone.onclick = popup.createApprovedOnClick();
				showOneSection('approval');
			} else if (response.data) {
				showOneSection('approval');
				updateEtag(response.data, queue, loading_id);
			} else {
				alert("OAuth error: " + response.oauthError + ": " + response.oauthErrorText);
			}
		}, params);
	}
	
	function getUserID() {
		var params = {};
		params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
		params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
		params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "netflix";
		params[gadgets.io.RequestParameters.OAUTH_USE_TOKEN] = "always";
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
		gadgets.io.makeRequest("http://api.netflix.com/users/current", function (response) {
			if (response.oauthApprovalUrl) {
				// user has not allowed access to Netflix
			} else if (response.data) {
				// get and store user id
				var itemList = response.data.getElementsByTagName("resource").item(0).childNodes;
				for (var i = 0; i < itemList.length ; i++) {
					if (itemList.item(i).nodeName != "#text") {
						if (itemList.item(i).nodeName == "link") {
							if (itemList.item(i).attributes.item(2).value == "current user") {
								var temp_url = itemList.item(i).attributes.item(0).value;
								var temp_user_id = temp_url.split("/");
								userID = temp_user_id[temp_user_id.length-1];
							}
						}
					}
				}
			} else {
				// The response.oauthError and response.oauthErrorText values may help debug
				// problems with your gadget.
				alert("OAuth error: " + response.oauthError + ": " + response.oauthErrorText);
			}
		}, params);
	}

	function selectSubTabs(id, tab) {
		var subTabs;
		if (tab == "browse") {
			subTabs = browse_sub_tabs;
		} else if (tab == "queue") {
			subTabs = queues_sub_tabs;
		}
		var el = _gel(id);
		if (el.className.indexOf("subTab_unselected__MODULE_ID__") != -1) {
			for (var i = 0; i < subTabs.length; i++) {
				var subTab = document.getElementById(subTabs[i]);
				if (id == subTabs[i]) {
					subTab.className = "subTab_selected__MODULE_ID__";
				} else {
					subTab.className = "subTab_unselected__MODULE_ID__";
				}
			}
		}	
	}
	
	function clearArea(object) {
		while (document.getElementById(object).hasChildNodes()) {
			document.getElementById(object).removeChild(document.getElementById(object).firstChild);
		}
	}
	
	/* -------------------------------------------------------------- */
	/* -------------------- Browse Methods -------------------------- */
	/* -------------------------------------------------------------- */
	browse_sub_tabs = ["search__MODULE_ID__", "top_100__MODULE_ID__", "new_releases__MODULE_ID__"];
	function selectSearch() {
		var browseSelected = -1;
		var i;
		for (i = 0; i < browse_sub_tabs.length; i++) {
			var browseTab = document.getElementById(browse_sub_tabs[i]).className;
			if (browseTab != "subTab_unselected__MODULE_ID__") {
				browseSelected = i;
			}
		}
		if (browseSelected == -1) {
			displaySearchForm(browse_sub_tabs[0]);
		}
	}
	
	function displaySearchForm(id) {
		selectSubTabs(id, "browse");
		clearArea("search_area");
		clearArea("results");
		
		var search_area = document.getElementById("search_area");
		
		var searchTextBox = document.createElement("input");
		searchTextBox.setAttribute("id", "Search");
		searchTextBox.setAttribute("type", "text");
		searchTextBox.setAttribute("onkeypress", "catchEnter(event);");
		search_area.appendChild(searchTextBox);	
		
		var submitButton = document.createElement("input");
		submitButton.setAttribute("value", "Search");
		submitButton.setAttribute("type", "submit");
		submitButton.setAttribute("onclick", "browse();");
		search_area.appendChild(submitButton);	
				
		var browse_sub_div = document.getElementById("browse_sub_div");
		browse_sub_div.className = "div_scroll_second_tab_search_bar";
		
		searchTextBox.focus();
	}
	
	function catchEnter(event) {
		var key = event.keyCode || event.which;
		if (key==13){
			browse();
		}
	}
	
	function getFeed(id, url) {
		selectSubTabs(id, "browse");
		clearArea("search_area");
		clearArea("results");
		var browse_sub_div = document.getElementById("browse_sub_div");
		browse_sub_div.className = "div_scroll_second_tab";
	
		document.getElementById("browse_tab_loading").className = "loading_div_front";
		_IG_FetchFeedAsJSON(url, function(feed) {
			if (feed == null){
				alert("There is no data.");
				return;
			}
			if (feed.Entry) {
				var catalogTable = document.createElement("table");
				catalogTable.setAttribute("id", "catalog_table");
				catalogTable.setAttribute("width", "100%");
				catalogTable.setAttribute("cellspacing", "0");
				catalogTable.setAttribute("cellpadding", "1");
				var headerRow = document.createElement("tr");
				headerRow.className = "table_header";
				
				if (feed.URL == top_100_rss) {
					var numberColumn = document.createElement("td");
					numberColumn.setAttribute("align", "center");
					numberColumn.setAttribute("width", "40px");
					numberColumn.appendChild(document.createTextNode("#"));
					headerRow.appendChild(numberColumn);					
				}
				
				var titleColumn = document.createElement("td");
				titleColumn.appendChild(document.createTextNode("Movie Title"));
				var addToQueueColumn = document.createElement("td");
				addToQueueColumn.appendChild(document.createTextNode("Add to Queue"));
				
				headerRow.appendChild(titleColumn);
				headerRow.appendChild(addToQueueColumn);
				catalogTable.appendChild(headerRow);
				var results_area = document.getElementById("results");
				
				for (var i = 0; i < feed.Entry.length; i++) {
					var movie_link = feed.Entry[i].Link.split("/");
					var url = "http://api.netflix.com/catalog/titles/movies/" + movie_link[movie_link.length - 1];
					
					var resultRow = document.createElement("tr");
					
					if (feed.URL == top_100_rss) {
						var listNumber = document.createElement("td");
						listNumber.setAttribute("align", "center");
						listNumber.appendChild(document.createTextNode(feed.Entry[i].Title.substring(0,3)));
						resultRow.appendChild(listNumber);					
					}
					
					var movieTitle = document.createElement("td");
					if (feed.URL == top_100_rss) {
						movieTitle.appendChild(document.createTextNode(feed.Entry[i].Title.substring(5)));
					} else {
						movieTitle.appendChild(document.createTextNode(feed.Entry[i].Title));
					}
					var addToQueue = document.createElement("td");	
					addToQueue.setAttribute("align", "center");
					var link = document.createElement("a");
					link.setAttribute("style", "cursor:pointer;cursor:hand;");
					link.setAttribute("onClick", "add_to_queue('" + url + "', 'disc', 'browse_tab_loading');");
					link.appendChild(document.createTextNode("+"));
					addToQueue.appendChild(link);
					
					resultRow.appendChild(movieTitle);
					resultRow.appendChild(addToQueue);
					catalogTable.appendChild(resultRow);
				}
				results_area.appendChild(catalogTable);
				document.getElementById("browse_tab_loading").className = "loading_div_back";
			}
		}, 100);
	}
		
	function browse() {
		document.getElementById("browse_tab_loading").className = "loading_div_front";
		fetchData("http://api.netflix.com/catalog/titles?expand=formats&term=" +  percentEncode(document.getElementById("Search").value), "catalog", "browse_tab_loading");
	}

	function showCatalog(response, loading_id) {
		clearArea("results");

		var catalogTable = document.createElement("table");
		catalogTable.setAttribute("width", "100%");
		catalogTable.setAttribute("cellspacing", "0");
		catalogTable.setAttribute("cellpadding", "1");
		var headerRow = document.createElement("tr");
		headerRow.className = "table_header";
		
		var titleColumn = document.createElement("td");
		titleColumn.appendChild(document.createTextNode("Movie Title"));
		var aveRatingColumn = document.createElement("td");
		aveRatingColumn.appendChild(document.createTextNode("Average Rating"));
		var addToQueueColumn = document.createElement("td");
		addToQueueColumn.appendChild(document.createTextNode("Add to Queue"));
		
		headerRow.appendChild(titleColumn);
		headerRow.appendChild(aveRatingColumn);
		headerRow.appendChild(addToQueueColumn);
		catalogTable.appendChild(headerRow);
		
		var results_area = document.getElementById("results");
		var itemList = response.getElementsByTagName("catalog_titles").item(0).childNodes;
		for (var i = 0; i < itemList.length ; i++) {	
			if (itemList.item(i).nodeName != "#text") {
				if (itemList.item(i).nodeName == "catalog_title") {	
					var instantAvailable = false;
					var nodeList = itemList.item(i).childNodes;
					for (var j = 0; j < nodeList.length ; j++) {
						var node = nodeList.item(j);
						if (node.nodeName != "#text") {
							if (node.nodeName == "id") {
								var movie_id = node.firstChild.nodeValue;
							}
							if (node.nodeName == "title") {
								var name = node.attributes.item("regular").value;
							}
							if (node.nodeName == "average_rating") {
								var aveRating = node.firstChild.nodeValue;
							}
							if (node.nodeName == "link" && node.attributes.item(2).value == "formats") {
								var formats = node.firstChild.childNodes;
								for (var k = 0; k < formats.length; k++) {
									if (formats.item(k).firstChild.attributes.item(2).value == "instant") {
										instantAvailable = true;
									}
								}
							}
						}
					}
					
					var resultRow = document.createElement("tr");
					var movieTitle = document.createElement("td");
					movieTitle.appendChild(document.createTextNode(name));
					
					var aveRatingCell = document.createElement("td");
					aveRatingCell.setAttribute("align", "center");
					aveRatingCell.appendChild(document.createTextNode(aveRating));
										
					var addToQueue = document.createElement("td");	
					addToQueue.setAttribute("align", "center");
					var link = document.createElement("a");
					link.setAttribute("style", "cursor:pointer;cursor:hand;");
					if (instantAvailable) {
						link.setAttribute("onClick", "add_to_both_queues('" + movie_id + "', 'browse_tab_loading');");
					} else {
						link.setAttribute("onClick", "add_to_queue('" + movie_id + "', 'disc', 'browse_tab_loading');");
					}
					link.appendChild(document.createTextNode("+"));
					addToQueue.appendChild(link);
					
					resultRow.appendChild(movieTitle);
					resultRow.appendChild(aveRatingCell);
					resultRow.appendChild(addToQueue);
					catalogTable.appendChild(resultRow);
				}
			}
		}
		results_area.appendChild(catalogTable);
		document.getElementById(loading_id).className = "loading_div_back";
	}
	
	/* -------------------------------------------------------------- */
	/* --------------------- Queue Methods -------------------------- */
	/* -------------------------------------------------------------- */
	queues_sub_tabs = ["disc__MODULE_ID__", "instant__MODULE_ID__"];
	
	function get_queue(type) {
		document.getElementById("queue_tab_loading").className = "loading_div_front";
		if (type == "disc") {
			fetchData("http://api.netflix.com/users/" + userID + "/queues/disc/available?max_results=500", "queue", "queue_tab_loading", "disc");
		} else if(type == "instant") {
			fetchData("http://api.netflix.com/users/" + userID + "/queues/instant?max_results=500", "queue", "queue_tab_loading", "instant");
		}
	}
	
	function queueToShow(id, type) {
		document.getElementById("q_results").className = "hide";
		selectSubTabs(id, "queue");
		
		get_queue(type);
		document.getElementById("q_results").className = "queue_results";
	}
	
	function updateQueueSelected() {
		var queueSelected = -1;
		var i;
		for (i = 0; i < queues_sub_tabs.length; i++) {
			var queueTab = document.getElementById(queues_sub_tabs[i]).className;
			if (queueTab != "subTab_unselected__MODULE_ID__") {
				queueSelected = i;
			}
		}
		if (queueSelected == -1) {
			queueToShow(queues_sub_tabs[0], "disc");
		} else {
			if (queueSelected == 0) {
				queueToShow(queues_sub_tabs[queueSelected], "disc");
			} else if (queueSelected == 1) {
				queueToShow(queues_sub_tabs[queueSelected], "instant");
			}
		}
	}
	
	function updateQueue(movieID, position, type, loading_id) {
		var currentQueueEtag;
		if (type == "disc") {
			currentQueueEtag = disc_etag;
		} else if (type == "instant") {
			currentQueueEtag = instant_etag;
		}
		var data = {
			title_ref : movieID, 
			position : position, 
			etag : currentQueueEtag
		};
		postData("http://api.netflix.com/users/" + userID + "/queues/" + type, data, type, loading_id);
	}
	
	function remove_from_queue(movieID, queue) {
		var currentQueue;
		if (queue == "disc") {
			currentQueue = disc_etag;
		} else if (queue == "instant") {
			currentQueue = instant_etag;
		}
	
		var data = {
			etag : currentQueue
		};
		url = movieID;
		
		var params = {};
		params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
		params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH;
		params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "netflix";
		params[gadgets.io.RequestParameters.OAUTH_USE_TOKEN] = "always";
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.DELETE;
		params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(data);
		gadgets.io.makeRequest(url, function (response) {
			if (response.oauthApprovalUrl) {
				var popup = shindig.oauth.popup({
					destination: response.oauthApprovalUrl + "&application_name=iGoogle+Netflix",
					windowOptions: null,
					onOpen: function() { showOneSection('waiting'); },
					onClose: function() { fetchData(url, type, loading_id, queue); }
				});
				var personalize = document.getElementById('personalize');
				personalize.onclick = popup.createOpenerOnClick();
				var approvaldone = document.getElementById('approvaldone');
				approvaldone.onclick = popup.createApprovedOnClick();
				showOneSection('approval');
			} else if (response.data) {
				showOneSection('approval');
				updateEtag(response.data, queue);
				updateQueueSelected();
			} else {
				// The response.oauthError and response.oauthErrorText values may help debug
				// problems with your gadget.
				alert("OAuth error: " + response.oauthError + ": " + response.oauthErrorText);
			}
		}, params);
	}
	
	function updatePosition(movie_id, row_id, queue_number, queue) {
		if (document.getElementById(row_id).className == "") {
			current_selection = movie_id;
			document.getElementById(row_id).className = "queue_highlight";
			for (var i = 1; i < movies_in_queue; i++) {
				if (i != queue_number) {
					document.getElementById(queue + "_queue_" + i + "_position_link").className = "hide";
					document.getElementById(queue + "_queue_" + i + "_update_position_link").className = "";
				}
			}
		} else {
			current_selection = null;
			document.getElementById(row_id).className = "";
			for (var i = 1; i < movies_in_queue; i++) {
				if (i != queue_number) {
					document.getElementById(queue + "_queue_" + i + "_position_link").className = "";
					document.getElementById(queue + "_queue_" + i + "_update_position_link").className = "hide";
				}
			}
		}
	}
	
	function moveToPosition(row_id, new_position, queue) {
		if (current_selection != null) {
			document.getElementById(row_id).className = "";
			updateQueue(current_selection, new_position, queue);
			current_selection = null;
			for (var i = 1; i < movies_in_queue; i++) {
				document.getElementById(queue + "_queue_" + i + "_position_link").className = "";
				document.getElementById(queue + "_queue_" + i + "_update_position_link").className = "hide";
			}
			updateQueueSelected();
		}
	}
	
	var movies_in_queue = null;
	function showQueue(response, queue, loading_id) {
		clearArea("q_results");
		
		movies_in_queue = 1;
		var queueTable = document.createElement("table");
		queueTable.setAttribute("width", "100%");
		queueTable.setAttribute("cellspacing", "0");
		queueTable.setAttribute("cellpadding", "1");
		var headerRow = document.createElement("tr");
		headerRow.className = "table_header";
		
		var updatePositionColumn = document.createElement("td");
		updatePositionColumn.appendChild(document.createTextNode("Update"));
		var queueColumn = document.createElement("td");
		queueColumn.appendChild(document.createTextNode("Order"));
		var topColumn = document.createElement("td");
		topColumn.appendChild(document.createTextNode("Top"));
		var titleColumn = document.createElement("td");
		titleColumn.appendChild(document.createTextNode("Movie Title"));
		var removeColumn = document.createElement("td");
		removeColumn.appendChild(document.createTextNode("Remove"));
		
		headerRow.appendChild(updatePositionColumn);
		headerRow.appendChild(queueColumn);
		headerRow.appendChild(topColumn);
		headerRow.appendChild(titleColumn);
		headerRow.appendChild(removeColumn);
		queueTable.appendChild(headerRow);
		
		var results_area = document.getElementById("q_results");
		var itemList = response.getElementsByTagName("queue").item(0).childNodes;
		for (var i = 0; i < itemList.length ; i++) {
			if (itemList.item(i).nodeName != "#text") {
				if (itemList.item(i).nodeName == "etag") {
					if (queue == "disc") {
						disc_etag = itemList.item(i).firstChild.nodeValue;
					} else if (queue == "instant") {
						instant_etag = itemList.item(i).firstChild.nodeValue;
					}
				}
				if (itemList.item(i).nodeName == "queue_item") {
					var nodeList = itemList.item(i).childNodes;
					for (var j = 0; j < nodeList.length ; j++) {
						var node = nodeList.item(j);
						if (node.nodeName != "#text") {
							if (node.nodeName == "id") {
								var movie_id = node.firstChild.nodeValue;
							}
							if (node.nodeName == "title") {
								var name = node.attributes.item("regular").value;
							}							
						}
					}
					
					var resultRow = document.createElement("tr");
					resultRow.setAttribute("id", queue + "_queue_" + movies_in_queue);
					
					var queuePosition = document.createElement("td");
					queuePosition.setAttribute("id", queue + "_queue_" + movies_in_queue + "_position");
					
					var queuePositionLink = document.createElement("a");
					queuePositionLink.setAttribute("id", queue + "_queue_" + movies_in_queue + "_position_link");
					queuePositionLink.setAttribute("style", "cursor:pointer;cursor:hand;");
					queuePositionLink.setAttribute("onClick", "updatePosition('" + movie_id + "', '" + queue + "_queue_" + movies_in_queue + "', " + movies_in_queue + ", '" + queue + "');");
					var upDownArrow = document.createElement("div");
					upDownArrow.innerHTML = "&#8597;";
					queuePositionLink.appendChild(upDownArrow);
					queuePosition.appendChild(queuePositionLink);
					
					var updateQueuePositionLink = document.createElement("a");
					updateQueuePositionLink.className = "hide";
					updateQueuePositionLink.setAttribute("id", queue + "_queue_" + movies_in_queue + "_update_position_link");
					updateQueuePositionLink.setAttribute("style", "cursor:pointer;cursor:hand;");
					updateQueuePositionLink.setAttribute("onClick", "moveToPosition('" + queue + "_queue_" + movies_in_queue + "', " + movies_in_queue + ", '" + queue + "');updateQueueSelected();");
					var rightArrow = document.createElement("div");
					rightArrow.innerHTML = "&#8658; ";
					updateQueuePositionLink.appendChild(rightArrow);
					queuePosition.appendChild(updateQueuePositionLink);
					
					var queueNumber = document.createElement("td");
					queueNumber.appendChild(document.createTextNode(movies_in_queue));
					
					var moveToTop = document.createElement("td");
					moveToTop.setAttribute("align", "center");
					var moveToTopLink = document.createElement("a");
					moveToTopLink.setAttribute("style", "cursor:pointer;cursor:hand;");
					moveToTopLink.setAttribute("onClick", "updateQueue('" + movie_id + "', 1, '" + queue + "');updateQueueSelected();");
					var upArrow = document.createElement("div");
					upArrow.innerHTML = "&#8679;";
					moveToTopLink.appendChild(upArrow);
					moveToTop.appendChild(moveToTopLink);
					
					var movieTitle = document.createElement("td");
					movieTitle.appendChild(document.createTextNode(name));
					
					var removeFromQueue = document.createElement("td");	
					removeFromQueue.setAttribute("align", "center");
					var link = document.createElement("a");
					link.setAttribute("style", "cursor:pointer;cursor:hand;");
					link.setAttribute("onClick", "remove_from_queue('" + movie_id + "','" + queue + "');");
					link.appendChild(document.createTextNode("x"));
					removeFromQueue.appendChild(link);
					
					resultRow.appendChild(queuePosition);
					resultRow.appendChild(queueNumber);
					resultRow.appendChild(moveToTop);
					resultRow.appendChild(movieTitle);
					resultRow.appendChild(removeFromQueue);
					queueTable.appendChild(resultRow);
										
					movies_in_queue++;
				}
			}
		}
		results_area.appendChild(queueTable);
		document.getElementById(loading_id).className = "loading_div_back";
	}
	
	/* -------------------------------------------------------------- */
	/* ---------------- Recommendation Methods ---------------------- */
	/* -------------------------------------------------------------- */	
	function recommendations_callback(tabId) {
		document.getElementById("recommendations_tab_loading").className = "loading_div_front";
		fetchData("http://api.netflix.com/users/" + userID + "/recommendations?expand=formats", "recommendations", "recommendations_tab_loading");
	}
		
	function showRecommendations(response, loading_id) {
		clearArea("r_results");
		
		var recommendationTable = document.createElement("table");
		recommendationTable.setAttribute("width", "100%");
		recommendationTable.setAttribute("cellspacing", "0");
		recommendationTable.setAttribute("cellpadding", "1");
		var headerRow = document.createElement("tr");
		headerRow.className = "table_header";
		
		var titleColumn = document.createElement("td");
		titleColumn.appendChild(document.createTextNode("Movie Title"));
		var predRatingColumn = document.createElement("td");
		predRatingColumn.appendChild(document.createTextNode("Predictive Rating"));
		var aveRatingColumn = document.createElement("td");
		aveRatingColumn.appendChild(document.createTextNode("Average Rating"));
		var addToQueueColumn = document.createElement("td");
		addToQueueColumn.appendChild(document.createTextNode("Add to Queue"));
		
		headerRow.appendChild(titleColumn);
		headerRow.appendChild(predRatingColumn);
		headerRow.appendChild(aveRatingColumn);
		headerRow.appendChild(addToQueueColumn);
		recommendationTable.appendChild(headerRow);
		
		var results_area = document.getElementById("r_results");
		var itemList = response.getElementsByTagName("recommendations").item(0).childNodes;
		for (var i = 0; i < itemList.length ; i++) {
			if (itemList.item(i).nodeName != "#text") {
				if (itemList.item(i).nodeName == "recommendation") {
					var instantAvailable = false;
					var nodeList = itemList.item(i).childNodes;
					for (var j = 0; j < nodeList.length ; j++) {
						var node = nodeList.item(j);
						if (node.nodeName != "#text") {
							if (node.nodeName == "id") {
								var movie_id = node.firstChild.nodeValue;
							}
							if (node.nodeName == "title") {
								var name = node.attributes.item("regular").value;
							}
							if (node.nodeName == "predicted_rating") {
								var predRating = node.firstChild.nodeValue;
							}
							if (node.nodeName == "average_rating") {
								var aveRating = node.firstChild.nodeValue;
							}
							if (node.nodeName == "link" && node.attributes.item(2).value == "formats") {
								var formats = node.firstChild.childNodes;
								for (var k = 0; k < formats.length; k++) {
									if (formats.item(k).firstChild.attributes.item(2).value == "instant") {
										instantAvailable = true;
									}
								}
							}
						}
					}
					
					var resultRow = document.createElement("tr");
					var movieTitle = document.createElement("td");
					movieTitle.appendChild(document.createTextNode(name));
					
					var predRatingCell = document.createElement("td");
					predRatingCell.setAttribute("align", "center");
					predRatingCell.appendChild(document.createTextNode(predRating));
					
					var aveRatingCell = document.createElement("td");
					aveRatingCell.setAttribute("align", "center");
					aveRatingCell.appendChild(document.createTextNode(aveRating));
										
					var addToQueue = document.createElement("td");	
					addToQueue.setAttribute("align", "center");
					var link = document.createElement("a");
					link.setAttribute("style", "cursor:pointer;cursor:hand;");
					if (instantAvailable) {
						link.setAttribute("onClick", "add_to_both_queues('" + movie_id + "', 'recommendations_tab_loading');");
					} else {
						link.setAttribute("onClick", "add_to_queue('" + movie_id + "', 'disc', 'recommendations_tab_loading');");
					}
					link.appendChild(document.createTextNode("+"));
					addToQueue.appendChild(link);
					
					resultRow.appendChild(movieTitle);
					resultRow.appendChild(predRatingCell);
					resultRow.appendChild(aveRatingCell);
					resultRow.appendChild(addToQueue);
					recommendationTable.appendChild(resultRow);
				}
			}
		}
		results_area.appendChild(recommendationTable);
		document.getElementById(loading_id).className = "loading_div_back";
	}
	